/*
 * redmicro all Copyright (c)
 *
 */

Rsd.define('Rsd.Faith.pages.BizZiJinShiYongShenQingPage', {
    extend: 'Rsd.container.Page',
    requires: [
        'Rsd.Faith.controls.XiaoQuInput',
        'Rsd.Faith.controls.XiaoQuListView',
        'Rsd.control.GridToolBar',
        'Rsd.control.PagingBar',
        'Rsd.control.TextArea'
    ],
    xtype: 'list-loudong',
    border: false,
    tabTitle: '小区楼栋',
    layout:'border',
    items:[
        {
            region: 'west',
            xtype:'list-xiaoqu',
            border:true,
            itemClick:'item_click',
            margin: '0 10 0 0',
            width:300
        },
        {
            xtype:'grid',
            region: 'center',
            pageIndexChanged:'pageIndexChanged',
            label:{
                xtype: 'grid-tool-bar',
                height:60,width:'100%',
                newButtons:[{text:'整个小区维修申请',handler:'btn_all'}],
                searchColumns:[{name:'LouDongHao',text:'楼栋号',op:'equals'},{name:'DanYuanHao',text:'单元号',op:'equals'}],
                searchHandler:'loadLouDongRows'
            },
            //footBar:{xtype:'paging-bar',indexChanged:'pageIndexChanged'},
            //{"XiaoQuBianHao":"7600","LouDongHao":null,"DanYuanCount":1,"BianHaoCount":1,"Xiao_qu_bian_hao":"7600","Lou_dong_hao":null}
            columns: [
                {
                    "text": "序号",
                    "xtype": "index",
                    "width": 40,
                    "sortable": false
                },
                {
                    "xtype": "ctrl-xiaoqu",
                    "name": "XiaoQuBianHao",
                    "text": "小区",
                    "sortable": false,
                    "width": 80,
                    "nobr": true
                },
                {
                    "xtype": "template",
                    "name": "LouDongHao ",
                    "text": "楼栋号",
                    "sortable": false,
                    align:'right',
                    "width": 200,
                    "format":"format_loudonghao",
                    "nobr": true
                },
                {
                    "xtype": "template",
                    "name": "DanYuanHao",
                    "text": "单元号",
                    align:'right',
                    "sortable": false,
                    "width": 100,
                    "format":"format_danyuanhao",
                    "precision": 0
                },
                {
                    "xtype": "number",
                    "name": "HuShu",
                    "text": "户数",
                    "precision": 0,
                    "sortable": false,
                    "width": 50,
                    "nobr": true
                },
                {
                    "name": "Remark",
                    "text": "备注",
                    "sortable": false,
                    "width": 80,
                    "nobr": true
                }

            ]

        }
    ],
    formFields: [
        {
            "name": "XiaoQuBianHao",
            "readOnly":true,
            "label": {
                "text": "小区编号",
                "width": 120
            },
            readOnly:true,
            "xtype": "ctrl-xiaoqu"
        },
        {
            "name": "LouDongHao",
            "readOnly":true,
            "label": {
                "text": "楼栋号",
                "width": 120
            },
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "DanYuanHao",
            "readOnly":true,
            "label": {
                "text": "单元号",
                "width": 120
            },
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "ShenQingRiQi",
            "label": {
                "text": "申请日期",
                "width": 120
            },
            "xtype": "date",
            "formatString": "yyyy-MM-dd",
            textChanged: 'ri_qi_changed'
        },
        {
            "name": "SuoShuQi",
            "label": {
                "text": "所属期",
                "width": 120
            },
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "ShenQingDanHao",
            //"readOnly":true,
            required:true,
            "label": {
                "text": "申请单号",
                "width": 120
            },
            "xtype": "text"
        },
        {
            "name": "ShenQingDanWei",
            required:true,
            "label": {
                "text": "申请单位",
                "width": 120
            },
            "xtype": "text"
        },
        {
            "name": "ShenQingDanWeiFaRenDaiBiao",
            "label": {
                "text": "申请单位法人代表",
                "width": 120
            },
            "xtype": "text"
        },
        {
            "name": "WeiXiuLeiXin",
            "label": {
                "text": "维修类型",
                "width": 120
            },
            "xtype": "combobox-enum",
            "enum": {
                "name": "Rsd.Faith.Core.WeiXiuLeiXin",
                "valueMember": "code",
                "items": [
                    {
                        "code": "YB",
                        "value": 0,
                        "text": "一般维修",
                        "color":'blue'
                    },
                    {
                        "code": "YJ",
                        "value": 1,
                        "text": "应急维修",
                        "color":'red'
                    }
                ]
            }
        },
        {
            "name": "ShenQingShiXiang",
            required:true,
            "label": {
                "text": "申请事项",
                "width": 120
            },
            "xtype": "text"
        },

        {
            "name": "ShenQingJinE",
            "textChanged":'jin_e_changed',
            required:true,
            "label": {
                "text": "申请金额",
                "width": 120
            },
            "xtype": "number",
            "precision": 2
        },
        {
            "name": "ShenQingJinEDaXie",
            "readOnly":true,
            "label": {
                "text": "申请金额大写",
                "width": 120
            },
            "xtype": "text"
        },
        {
            "name": "WeiXiuFanWei",
            required:true,
            "label": {
                "text": "维修范围",
                "width": 120
            },
            height: 60,
            "xtype": "textarea"
        },

        {
            "name": "ShouKuanYinHang",
            "label": {
                "text": "收款银行",
                "width": 120
            },
            "xtype": "text"
        },
        {
            "name": "ShouKuanYinHangZhangHao",
            "label": {
                "text": "收款银行账号",
                "width": 120
            },
            "xtype": "text"
        },
      /*  {
            "name": "BoFuFangShi",
            "label": {
                "text": "拨付方式",
                "width": 120
            },
            "xtype": "text"
        },*/
     /*   {
            "name": "HuaBoZhuangTai",
            "label": {
                "text": "划拨状态",
                "width": 120
            },
            "xtype": "text"
        },*/
     /*   {
            "name": "BoFuHouZhuanHuYuE",
            "label": {
                "text": "拨付后余额",
                "width": 120
            },
            "xtype": "number",
            "precision": 2
        },*/
        {
            "name": "Remark",
            "label": {
                "text": "备注",
                "width": 120
            },
            "xtype": "text"
        }
    ],
    /*
    * */
    constructor: function (config) {
        config = config || {};
        Rsd.apply(this, config);


        var _grid = this.items[1];
        _grid.dataSource = Rsd.app.getService('info.getloudonglist');
    },
    load:function () {
        var me = this;
        setTimeout(function () {
            me.items[0].loadData();
        },50);
    },
    item_click:function(item)
    {
        var me = this;
        me.__xiao_qu_bian_hao = item.content.key;
        me.loadLouDongRows();
    },

    format_loudonghao:function format_loudonghao(row)
    {
        var me = this;
        var _fn =function () {
            var now = new Date();
            var danHao = (now.getTime()-new Date('1995-01-01').getTime())*10000 + Math.random()*10000;

            var data = {
                XiaoQuBianHao:row['XiaoQuBianHao'],
                LouDongHao:row['LouDongHao'],
                ShenQingDanHao:danHao,
                ShenQingRiQi:now.format('yyyy-MM-dd'),
                SuoShuQi:now.format('yyyy'),
                HuaBoZhuangTai:'DJ',
                WeiXiuFanWei:row['LouDongHao'] + '栋'
            };
            var list = me.formFields.slice(0);
            list.splice(2,1);
            Rsd.app.showModelView('Rsd.Faith.Models.UsingZiJinShiYongShenQingBiaoModel',list,data,'房屋维修划拨申请',['save'],false,me);
        };

        var _fn_1 =function () {
            alert('操作权限限制');
        };

        var _fn2 =function () {
            var now = new Date();
            var danHao = (now.getTime()-new Date('1995-01-01').getTime())*10000 + Math.random()*10000;

            var data = {
                XiaoQuBianHao:row['XiaoQuBianHao'],
                LouDongHao:row['LouDongHao'],
                ShenQingDanHao:danHao,
                ShenQingRiQi:now.format('yyyy-MM-dd'),
                SuoShuQi:now.format('yyyy'),
                HuaBoZhuangTai:'DJ',
                WeiXiuFanWei:row['LouDongHao'] + '栋'
            };

            var list = me.formFields.slice(0);
            list.splice(2,1);
            Rsd.app.showModelView('Rsd.Faith.Models.UsingZiJinShiYongShenQingDtBiaoModel',list,data,'电梯/消防维修划拨申请',['save'],false,me);
        };

        return [
            Rsd.text(row['LouDongHao']),
            Rsd.blankspan(10),
            Rsd.button('房屋维修申请',_fn,null,null,{backgroundColor:'green',}),
            Rsd.blankspan(2),
            Rsd.button('电梯/消防维修申请',_fn2,null,null,{}),
            Rsd.blankspan(2),
            Rsd.button('退伙申请',_fn_1,null,null,{backgroundColor:'#cc7f20'})
        ];
    },
    /*
    * */
    format_danyuanhao:function(row){
        var me = this;
        var _fn =function () {
            var now = new Date();
            var danHao = (now.getTime()-new Date('1995-01-01').getTime())*10000 + Math.random()*10000;

            var data = {
                XiaoQuBianHao:row['XiaoQuBianHao'],
                LouDongHao:row['LouDongHao'],
                DanYuanHao:row['DanYuanHao'],
                ShenQingDanHao:danHao,
                ShenQingRiQi:now.format('yyyy-MM-dd'),
                SuoShuQi:now.format('yyyy'),
                HuaBoZhuangTai:'DJ',
                WeiXiuFanWei: row['LouDongHao']+ '栋' + row['DanYuanHao'] + '单元'};

            Rsd.app.showModelView('Rsd.Faith.Models.UsingZiJinShiYongShenQingBiaoModel',me.formFields,data,'房屋维修划拨申请',['save'],false,me);
        };

        return [Rsd.text(row['DanYuanHao']),Rsd.blankspan(10),Rsd.button('房屋维修申请',_fn,null,null,{backgroundColor:'green'})];
    },
    btn_all:function btn_all() {

        var me = this;
        var  bian_hao= this.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }
        var now = new Date();
        var danHao = (now.getTime()-new Date('1995-01-01').getTime())*10000 + Math.random()*10000;

        var data = {
            XiaoQuBianHao:bian_hao,
            ShenQingDanHao:danHao,
            ShenQingRiQi:now.format('yyyy-MM-dd'),
            SuoShuQi:now.format('yyyy'),
            HuaBoZhuangTai:'DJ',
            WeiXiuFanWei:'整个小区'
        };

        var list = me.formFields.slice(0);
        list.splice(1,2);
        Rsd.app.showModelView('Rsd.Faith.Models.UsingZiJinShiYongShenQingBiaoModel',list,data,'申请划拨',['save'],false,me);
    },
    /*
     * */
    loadLouDongRows:function loadLouDongRows( ) {

        var me = this;
        var  bian_hao= me.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }

        var grid = me.items[1];

        Rsd.showWaiting(this.id,grid);
        var args = {xiaoqubianhao:bian_hao};
        var where =  grid.toolBar.getWhere() ||[];
        for(var i in where)
        {
            if(where[i].name =="LouDongHao")
            {
                args['LouDongHao'] = where[i].value;
                continue;
            }
            if(where[i].name =="DanYuanHao")
            {
                args['DanYuanHao'] = where[i].value;
                continue;
            }
        }
        grid.loadData(args,function(data ){

            Rsd.closeWaiting(me.id);
        });

    },
    /*
    * */
    ri_qi_changed:function ri_qi_changed(sender,e)
    {
        sender.parent.parent.parent.setFieldValue('SuoShuQi',sender.getValue().substr(0,4));
    },
    /*
    * */
    jin_e_changed:function (sender,e) {

        sender.parent.parent.parent.setFieldValue('ShenQingJinEDaXie',Rsd.formatCurrencyCN(sender.getValue()));
    }

});
